#include "MouseCursorManager.h"
#include "Platform/PlatformInterface.h"
#include "Object/NewObject.h"

static RMouseCursorManager* Instance=nullptr;

RMouseCursorManager* RMouseCursorManager::Get()
{
    return Instance;
}

RMouseCursorManager::RMouseCursorManager()
{
    Instance=this;
}

void RMouseCursorManager::Initialize()
{
    for(int CursorType=0;CursorType<int(EMouseCursorType::SystemCursorTypeNum);CursorType++)
    {
        CreateSystemCursor(EMouseCursorType(CursorType));
    }

    UseDefaultCursor();
}

void RMouseCursorManager::CreateSystemCursor(EMouseCursorType InType)
{
    auto Cursor=NewObject<RMouseCursor>(this);
    Cursor->Type=InType;
    SystemCursors.Add(InType,Cursor);
}

void RMouseCursorManager::UseCursor(RMouseCursorPtr InCursor)
{
    CurrentCursor=InCursor;
    IPlatformInterface::GetPlatformInterface()->UseSystemCursor(CurrentCursor->Type);


}

void RMouseCursorManager::UseCursor(EMouseCursorType InType)
{
    //cant check CurrentCursor->Type== InType,
    // because the cursor may be changed by operating system

    assert(SystemCursors.Contains(InType)
    && "Only accept system orignal curosr now" );

    UseCursor(SystemCursors[InType]);
}

void RMouseCursorManager::UseDefaultCursor()
{
    UseCursor(SystemCursors[EMouseCursorType::Arrow]);
}